﻿using Convertor.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.SortSqlScript
{
    public class Plugin : PluginBase
    {
        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.SortSqlScript;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.SortSqlScript;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Database") + " - " + UILanguage.FindResource("Plugin_SortSqlScript_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_SortSqlScript_Desc"); }
        }

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            string input2 = content.OutputContent.ToString();
            Regex reg = new Regex(@"\r\n-- =+?.*[^.]*-- =+?\r\n", RegexOptions.Multiline);
            input2 = reg.Replace(input2, "\r\n");
            int alterTablePosition = input2.IndexOf("ALTER TABLE", 0);
            if (alterTablePosition == -1)
            {
                content.OutputContent = new StringBuilder(input2);
                return content;
            }
            string toSplit = input2.Substring(0, alterTablePosition);
            string alterTable = input2.Remove(0, alterTablePosition); //约束字符串

            string[] creatTables = Regex.Split(toSplit, "CREATE TABLE", RegexOptions.IgnoreCase);
            if (creatTables.Length <= 1)
            {
                content.OutputContent = new StringBuilder(input2);
                return content;
            }

            List<string> orderTables = new List<string>();

            for (int i = 1; i < creatTables.Length; i++)
            {
                string[] creatTables2 = Regex.Split(creatTables[i], "CREATE TRIGGER", RegexOptions.IgnoreCase);
                orderTables.Add("CREATE TABLE" + creatTables2[0]);
                if (creatTables2.Length >= 1)
                {
                    for (int j = 1; j < creatTables2.Length; j++)
                    {
                        orderTables.Add("CREATE TRIGGER" + creatTables2[j]);
                    }
                }
            }
            //string lastStr = creatTables[creatTables.Length - 1];
            //int alterTablePosition = lastStr.IndexOf("ALTER TABLE", 0);


            var words_1 = from word in orderTables orderby word select word;

            StringBuilder sb = new StringBuilder();
            sb.Append(creatTables[0]);
            foreach (var i in words_1)
            {
                if (i.ToString().Length != 0)
                {
                    sb.Append(i.ToString());
                }
            }
            sb.Append(alterTable);

            if (Log != null) { Log(this, UILanguage.FindResource("Plugin_SortSqlScript_ConvertSuccess"), LogTypes.Normal); }

            return new ConvertParamters(content.LastInputContent, sb);
        }

        #endregion
    }
}
